﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Deanery.Controller;
using Deanery.Controller.Directories;
using Deanery.Controller.StudyData;

namespace Deanery.View
{
    public partial class DirectoryForm : Form
    {
        /// <param>hold table name
        ///     <name>tbName</name>
        /// </param>
        private string tbName;


        public DirectoryForm(string tableName)
        {
            InitializeComponent();
            Text = tableName;
            tbName = tableName;
            RefreshDataGrid(tableName);
        }

        /// <summary>
        /// This method does DataGrid refresh and provides ukrainian name to the columns
        /// </summary>
        /// <param name="tableName">string with table name</param>
        private void RefreshDataGrid(string tableName)
        {
            dataGridView1.DataSource = FillDataGrid(tableName);
            GetColumnName(tableName);
        }


        public override sealed string Text
        {
            get { return base.Text; }
            set { base.Text = value; }
        }

        /// <summary>
        /// This method take all data from directories tables
        /// </summary>
        /// <param name="name">string with table name</param>
        /// <returns>IQueryable with selected data</returns>
        private IQueryable FillDataGrid(string name)
        {
            switch (name)
            {
                case "ECTS_Ocinka":
                    {
                        var obj = new ECTSOcinkaController();
                        return obj.GetAll();
                    }
               
                case "Faculty":
                    {
                        var obj = new FacultyController();
                        return obj.GetAll();
                    }
               
                case "Privilege_Group":
                    {
                        var obj = new PrivilegeGroupController();
                        return obj.GetAll();
                    }
                case "Qualification":
                    {
                        var obj = new QualificationController();
                        return obj.GetAll();
                    }

                case "Region":
                    {
                        var obj = new RegionController();
                        return obj.GetAll();
                    }
                case "Semester":
                    {
                        var obj = new SemesterController();
                        //FormGridView(obj.ColumnName());
                        return obj.GetAll();
                    }
                case "Sex":
                    {
                        var obj = new SexController();
                        return obj.GetAll();
                    }
                case "Status":
                    {
                        var obj = new StatusController();
                        return obj.GetAll();
                    }
                case "Type_City":
                    {
                        var obj = new TypeCityController();
                        return obj.GetAll();
                    }
                case "Type_Contacts":
                    {
                        var obj = new TypeContactsController();
                        return obj.GetAll();
                    }
                case "Type_Documents":
                    {
                        var obj = new TypeDocumentsController();
                        return obj.GetAll();
                    }
                case "Type_Education":
                    {
                        var obj = new TypeEducationController();
                        return obj.GetAll();
                    }
                case "Type_Orders":
                    {
                        var obj = new TypeOrdersController();
                        return obj.GetAll();
                    }
                case "Type_Payment":
                    {
                        var obj = new TypePaymentController();
                        return obj.GetAll();
                    }
                case "Type_Privilege":
                    {
                        var obj = new TypePrivilegeController();
                        return obj.GetAll();
                    }
                case "Type_Street":
                    {
                        var obj = new TypeStreetController();
                        return obj.GetAll();
                    }
                
                case "Type_Test":
                    {
                        var obj = new TypeTestController();
                        return obj.GetAll();
                    }
                case "Napryam":
                    {
                        var obj = new NapryamController();
                        return obj.GetAll();
                    }
                case "Subject":
                    {
                        var obj = new SubjectController();
                        return obj.GetAll();
                    }
                default:
                    MessageBox.Show(@"No such element!");
                    break;
            }
            
            return null;
        }

        /// <summary>
        /// This method take columns name in ukrainian
        /// and cause FormatingGridView and transfers there these names
        /// </summary>
        /// <param name="name">string with table name</param>
        private void GetColumnName(string name)
        {
            switch (name)
            {
                case "ECTS_Ocinka":
                    {
                        var obj = new ECTSOcinkaController();
                        FormationGridView(obj.ColumnName());
                        break;
                    }

                case "Faculty":
                    {
                        var obj = new FacultyController();
                        FormationGridView(obj.ColumnName());
                        break;
                    }
                case "Privilege_Group":
                    {
                        var obj = new PrivilegeGroupController();
                        FormationGridView(obj.ColumnName());
                        break;
                    }
                case "Qualification":
                    {
                        var obj = new QualificationController();
                        FormationGridView(obj.ColumnName());
                        break;
                    }
                case "Region":
                    {
                        var obj = new RegionController();
                        FormationGridView(obj.ColumnName());
                        break;
                    }
                case "Semester":
                    {
                        var obj = new SemesterController();
                        FormationGridView(obj.ColumnName());
                        break;
                    }
                case "Sex":
                    {
                        var obj = new SexController();
                        FormationGridView(obj.ColumnName());
                        break;
                    }
                case "Status":
                    {
                        var obj = new StatusController();
                        FormationGridView(obj.ColumnName());
                        break;
                    }
                case "Type_City":
                    {
                        var obj = new TypeCityController();
                        FormationGridView(obj.ColumnName());
                        break;
                    }
                case "Type_Contacts":
                    {
                        var obj = new TypeContactsController();
                        FormationGridView(obj.ColumnName());
                        break;
                    }
                case "Type_Documents":
                    {
                        var obj = new TypeDocumentsController();
                        FormationGridView(obj.ColumnName());
                        break;
                    }
                case "Type_Education":
                    {
                        var obj = new TypeEducationController();
                        FormationGridView(obj.ColumnName());
                        break;
                    }
                case "Type_Orders":
                    {
                        var obj = new TypeOrdersController();
                        FormationGridView(obj.ColumnName());
                        break;
                    }
                case "Type_Payment":
                    {
                        var obj = new TypePaymentController();
                        FormationGridView(obj.ColumnName());
                        break;
                    }
                case "Type_Privilege":
                    {
                        var obj = new TypePrivilegeController();
                        FormationGridView(obj.ColumnName());
                        break;
                    }
                case "Type_Street":
                    {
                        var obj = new TypeStreetController();
                        FormationGridView(obj.ColumnName());
                        break;
                    }
                case "Type_Test":
                    {
                        var obj = new TypeTestController();
                        FormationGridView(obj.ColumnName());
                        break;
                    }
                case "Napryam":
                    {
                        var obj = new NapryamController();
                        FormationGridView(obj.ColumnName());
                        break;
                    }
                case "Subject":
                    {
                        var obj = new SubjectController();
                        FormationGridView(obj.ColumnName());
                        break;
                    }
            }

        }
        
        /// <summary>
        /// This method givse header names to DataGrid and resizes columns width
        /// </summary>
        /// <param name="nameList">names for columns in uktainian</param>
        private void FormationGridView(Array nameList)
        {
            var index = 0;
            foreach (var name in nameList)
            {
                dataGridView1.Columns[index].HeaderText = name.ToString();
                index++;
            }
            dataGridView1.Columns[0].Visible = false;
            dataGridView1.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToDisplayedHeaders);
        }

        /// <summary>
        /// This method forms reccord which was select by user
        /// </summary>
        /// <returns>ArrayList(every item - cell value of the selected reccord)</returns>
        private ArrayList GetSelectedReccord()
        {
            var reccord = new ArrayList();
            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
                for (int i = 0; i < row.Cells.Count; i++)
                {
                    reccord.Add(row.Cells[i].Value);
                }
            return reccord;
        }

        //PROBLEM: with refresh it works every time, even tno data changed
        private void btnInsert_Click(object sender, EventArgs e)
        {
           Form frm = new DirectoriesDialogForm(tbName, dataGridView1.Columns.Count - 1, null);
            var dialogResult = frm.ShowDialog();
            //if (dialogResult == DialogResult.OK)
            //    RefreshDataGrid(tbName);
            RefreshDataGrid(tbName);
               
        }

        //PROBLEM: with refresh it works every time, even tno data changed
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            Form frm = new DirectoriesDialogForm(tbName, dataGridView1.Columns.Count - 1, GetSelectedReccord());
            var dialogResult = frm.ShowDialog();
            //if (dialogResult == DialogResult.OK)
            //{ 
            //    RefreshDataGrid(tbName);
            //    MessageBox.Show("here");
            //}
            RefreshDataGrid(tbName);
            btnUpdate.Enabled = false;
        }

        /// <summary>
        /// This method changes button Update enabled 
        /// cheks whether the selected no more that 1 reccord
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            btnUpdate.Enabled = dataGridView1.SelectedRows.Count == 1;
        }
    }
}
